package com.ruoyi.workbench.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * @author hanjiafeng
 * @date 2020年10月19日
 */
public class DateUtil {
    /**
     * 计算是距离上次提醒过了7天
     *
     * @param endDate 到期日期
     * @return 是否
     */
    public static boolean isExpired(Date endDate) {
        long lastNoticeMills = endDate.getTime();
        long dayMills = (long) 7 * 24 * 60 * 60 * 1000;
        Date noticeDate = new Date(lastNoticeMills + dayMills);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Date today = calendar.getTime();

        return noticeDate.before(today);
    }

    /**
     * 检测给定过期日期是否在给定月份之后过期
     *
     * @param expireDate 过期日期
     * @param month      月份
     * @return 是否过期
     */
    public static boolean isExpiredByMonth(Date expireDate, int month) {
        Calendar futureDate = Calendar.getInstance();
        futureDate.setTime(new Date(System.currentTimeMillis()));
        futureDate.add(Calendar.MONTH, month);
        Calendar expireCalendar = Calendar.getInstance();
        expireCalendar.setTime(expireDate);
        return futureDate.compareTo(expireCalendar) > 0;
    }

    /**
     * 计算给定时间和现在差几个月
     *
     * @param d1 d1
     * @return 差值
     */
    public static double getMonths(Date d1) {

        double m1 = (double) d1.getTime();
        double m2 = (double) System.currentTimeMillis();

        double difference = (m1 - m2) / 1000 / 60 / 60 / 24 / 30;
        return Math.round(difference * 10) / (double) 10;
    }
}
